package gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.border.LineBorder;

import db.domain.Student;

/**
 * 
 * 学生管理系统主界面
 * 
 * @author 冀鲁
 * @date 2011-06-26
 * @version 1.0.0.0
 * 
 */
public class StudentInfoForm extends JFrame implements ActionListener,
		MouseListener {
	// 菜单栏
	private JMenuBar mBar = new JMenuBar();
	// 创建菜单
	private JMenu menu01 = new JMenu("学生信息");
	private JMenu menu02 = new JMenu("帮助");
	// 菜单项
	private JMenuItem menu01_01 = new JMenuItem("添加");
	private JMenuItem menu01_02 = new JMenuItem("查询");
	private JMenuItem menu01_03 = new JMenuItem("修改");
	private JMenuItem menu01_04 = new JMenuItem("删除");
	private JMenuItem menu01_05 = new JMenuItem("退出");

	private JMenuItem menu02_01 = new JMenuItem("关于");
	private JLabel lblTitle;
	private JLabel lblsubTitle;
	private JButton btnAdd; // 添加
	private JButton btnQuery; // 查询
	private JButton btnModify; // 修改
	private JButton btnDelete; // 删除
	/* 自己添加 */
	private JLabel lblPicture;// 系统图片
	private Student Student;

	public StudentInfoForm(Student student) {
		// 位置变量
		int width, height, btnwidth, btnheight, btn_x, btn_y, btn_yy;
		width = 550;
		height = 300;
		btnwidth = 120;
		btnheight = 40;
		btn_x = width - btnwidth - 30;// 按钮与右边框的距离
		btn_y = btnheight + 10;// 按钮与上边框的距离
		btn_yy = 10;// 按钮间距

		this.setTitle("校园卡管理系统-学生主界面");
		// 添加菜单
		menu01.add(menu01_01);
		menu01.add(menu01_02);
		menu01.add(menu01_03);
		menu01.add(menu01_04);
		menu01.addSeparator();// 分割线
		menu01.add(menu01_05);

		menu02.add(menu02_01);

		mBar.add(menu01);
		mBar.add(menu02);

		this.setJMenuBar(mBar);
		this.setLayout(null);// 坐标布局

		/* 按钮 */
		// 添加按钮
		btnAdd = new JButton("添加信息");
		Font font1 = new Font("黑体", Font.BOLD, 20);
		btnAdd.setFont(font1);
		btnAdd.setBounds(btn_x, btn_y + 0 * (btnheight + btn_yy), btnwidth,
				btnheight);
		btnAdd.addMouseListener(this);
		btnAdd.addActionListener(this);
		btnAdd.setVisible(false);
		this.add(btnAdd);
		// 删除按钮
		btnDelete = new JButton("删除信息");
		btnDelete.setFont(font1);
		btnDelete.setBounds(btn_x, btn_y + 1 * (btnheight + btn_yy), btnwidth,
				btnheight);
		btnDelete.addMouseListener(this);
		btnDelete.addActionListener(this);
		btnDelete.setVisible(false);
		this.add(btnDelete);
		// 修改按钮
		btnModify = new JButton("修改信息");
		btnModify.setFont(font1);
		btnModify.setBounds(btn_x, btn_y + 2 * (btnheight + btn_yy), btnwidth,
				btnheight);
		btnModify.addMouseListener(this);
		btnModify.addActionListener(this);
		this.add(btnModify);
		// 查询按钮
		btnQuery = new JButton("查询信息");
		btnQuery.setFont(font1);
		btnQuery.setBounds(btn_x, btn_y + 3 * (btnheight + btn_yy), btnwidth,
				btnheight);
		btnQuery.addMouseListener(this);
		btnQuery.addActionListener(this);
		this.add(btnQuery);

		// 设置图片的位置
		lblPicture = new JLabel("<html>　　系<br>　　统<br>　　图<br>　　片<br></html>");
		// lblPicture.setBorder(new LineBorder(Color.blue, 5, true));
		lblPicture.setBounds(0, 0, width, height);// 设置图片位置
		displayPictur displayPictur = new displayPictur(1, lblPicture);
		this.add(lblPicture);

		Student = student;

		// 设置窗口的大小
		this.setSize(width, height + 60);

		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				closeWindow();
			}

		});
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.setResizable(false);
		this.setVisible(true);

	}

	/**
	 * 关闭窗口提示
	 */
	protected void closeWindow() {
		// 弹出一个确认对话框
		int r = JOptionPane.showConfirmDialog(this, "你确认要关闭系统吗？", "关闭窗口提示",
				JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
		if (r == JOptionPane.YES_OPTION) {
			System.exit(0);
		}
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnAdd) { // 添加
			new StudentManagerForm("add", Student);
		} else if (e.getSource() == btnDelete) {// 删除
			new StudentManagerForm("delete", Student);
		} else if (e.getSource() == btnQuery) {// 查询
			new StudentManagerForm("query", Student);
		} else if (e.getSource() == btnModify) {// 修改
			new StudentManagerForm("modify", Student);
		}
	}

	// 鼠标点击事件
	public void mouseClicked(MouseEvent e) {

	}

	// 鼠标进入事件
	public void mouseEntered(MouseEvent e) {
		((JComponent) e.getSource()).setForeground(Color.red);
	}

	// 鼠标离开事件
	public void mouseExited(MouseEvent e) {
		((JComponent) e.getSource()).setForeground(Color.black);
	}

	// 鼠标按下事件（左键，右键）
	public void mousePressed(MouseEvent e) {
		((JComponent) e.getSource()).setForeground(Color.green);
	}

	// 鼠标按键释放事件
	public void mouseReleased(MouseEvent e) {
		((JComponent) e.getSource()).setForeground(Color.yellow);
	}
}
